﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.IO;
using System.Text;

namespace Megaman
{
    class Background
    {
        Layer[,] backgrounds;
        String basePath;
        ContentManager Content;
        int width;
        int height;

        public Background(String basePath, ContentManager Content)
        {
            this.basePath = basePath;
            this.Content = Content;
            loadBackgrounds();
        }
        public void Draw(SpriteBatch sp, Vector2 CameraPosition)
        {
            List<Vector2> layersToDraw = getLayersToDraw(CameraPosition, sp.GraphicsDevice.Viewport);
            foreach (Vector2 layer in layersToDraw)
            {
                try
                {
                    backgrounds[(int)layer.X, (int)layer.Y].Draw(sp, CameraPosition,
                        new Vector2(layer.X * sp.GraphicsDevice.Viewport.Width,
                            layer.Y * sp.GraphicsDevice.Viewport.Height));
                }
                catch (IndexOutOfRangeException)
                {
                }
            }
        }

        public List<Vector2> getLayersToDraw(Vector2 CameraPosition, Viewport v)
        {
            int i = (int)Math.Floor(CameraPosition.X / v.Width);
            int j = (int)Math.Floor(CameraPosition.Y / v.Height);
            int k = (int)Math.Ceiling(CameraPosition.X / v.Width);
            int l = (int)Math.Ceiling(CameraPosition.Y / v.Height);

            List<Vector2> array = new List<Vector2>();
            array.Add(new Vector2(i, j));
            if (i != k && j != l)
            {
                array.Add(new Vector2(k, l));
                array.Add(new Vector2(k, j));
                array.Add(new Vector2(i, l));

            }
           if (i == k && j != l )
               array.Add(new Vector2(i,l));       
           if (j == l && i != k)
               array.Add(new Vector2(k,j));



            return array;

        }
        public void loadBackgrounds()
        {
            String data = String.Format(basePath + "\\Scene\\Background\\data.txt");
            data = Path.Combine(StorageContainer.TitleLocation, "Content/"+data);

            String path = String.Format(basePath + "\\Scene\\Background\\");
            path = Path.Combine(StorageContainer.TitleLocation, "Content/"+path);

            Dictionary<int, Layer> Layers = new Dictionary<int, Layer>();
            List<String> lines = new List<string>();
            int i = 0;
            int j = 0;
            using (StreamReader reader = new StreamReader(data))
            {
                string line;
                while( (line = reader.ReadLine()) != null )
                {
                    foreach (char c in line)
                    {
                        if (char.IsDigit(c))
                        {
                            Vector2 ScrollRate = getScrollRate(path + c + "\\data.txt");
                            try
                            {
                                Layers.Add(int.Parse(c.ToString()), new Layer(Content, path + c + "\\",
                                    getScrollRate(path + c + "\\data.txt"),c));
                            }
                            catch (ArgumentException)
                            {

                            }
                        }
                        i++;
                    }
                    lines.Add(line);
                    j++;
                    i = 0;
                }
 

            }
            width = lines[0].Length;
            height = j;
            backgrounds = new Layer[width, height];
            for (j = 0; j < height; j++)
            {
                for (i = 0; i < width; i++)
                {
                    backgrounds[i, j] = Layers[int.Parse(lines[j][i].ToString())];
                }
            }
        }

        private Vector2 getScrollRate(String path)
        {
            Vector2 resp = new Vector2();
            using (StreamReader reader = new StreamReader(path))
            {
                try
                {
                    resp.X = float.Parse(reader.ReadLine());
                }
                catch (Exception)
                {
                    resp.X = 0;
                }
                try
                {
                    resp.Y = float.Parse(reader.ReadLine());
                }
                catch (Exception)
                {
                    resp.Y = 0;
                }
            }
            return resp;
        }
    }
}
